package cn.edu.scnu.Admin.controller;

import cn.edu.scnu.Admin.service.AdminService;
import cn.edu.scnu.annotation.IgnoreToken;
import cn.edu.scnu.annotation.NeedAdmin;
import cn.edu.scnu.pojo.MatchActivity;
import cn.edu.scnu.pojo.Organizer;
import cn.edu.scnu.vo.OrganizerVO;
import cn.edu.scnu.vo.PersonalUserVO;
import cn.edu.scnu.vo.ResultCode;
import cn.edu.scnu.vo.SysResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisCluster;

import java.util.List;

@Api
@RestController
public class AdminController {
    @Autowired
    private AdminService adminService;


    /**
     * @return 所有赛事列表
     */
    @IgnoreToken
    @NeedAdmin
    @ApiOperation("获取所有赛事活动")
    @GetMapping("/admins/manage/getmatches")
    public SysResult getAllmatches() {
        SysResult result;
        try {
            List<MatchActivity> matches = adminService.getAllmatches();
            result = SysResult.build(ResultCode.SUCCESS, "获取所有赛事成功", matches);
        } catch (Exception e) {
            result = SysResult.build(ResultCode.FAIL, e.getMessage(), null);
        }
        return result;
    }


    /**
     * 查询组织用户
     * @return 组织用户表
     */
    @IgnoreToken
    //@NeedAdmin
    @ApiOperation("获取所有组织用户")
    @GetMapping("/admins/manage/getOrganizer")
    public SysResult getOrganizer() {
        SysResult result;
        try {
            List<OrganizerVO> organizer = adminService.getAllOrganizer();
            result = SysResult.build(ResultCode.SUCCESS, "获取所有组织用户信息成功", organizer);
        } catch (Exception e) {
            result = SysResult.build(ResultCode.FAIL, e.getMessage(), null);
        }
        return result;
    }
    /**
     * 查询个人用户
     * @return 个人用户表
     */
    @IgnoreToken
//    @NeedAdmin
    @ApiOperation("获取所有个人用户")
    @GetMapping("/admins/manage/getPersonalUser")
    public SysResult getPersonalUser() {
        SysResult result;
        try {
            List<PersonalUserVO> personalUser = adminService.getAllPersonalUser();
            result = SysResult.build(ResultCode.SUCCESS, "获取所有个人用户信息成功", personalUser);
        } catch (Exception e) {
            result = SysResult.build(ResultCode.FAIL, e.getMessage(), null);
        }
        return result;
    }

    /**
     * 下架比赛
     * @param matchId
     *
     */
    @IgnoreToken
//    @NeedAdmin
    @ApiOperation("下架比赛")
    @ApiImplicitParam(name = "matchId", value = "赛事活动id", dataType = "String", paramType = "body", required = true)
    @PutMapping("/admins/manage/modifyState")
    public SysResult modifyState(@RequestBody String matchId){
        try{
            adminService.modifyState(matchId);
            return SysResult.build(ResultCode.CREATED,"赛事状态修改成功",null);
        }catch (Exception e){
            return SysResult.build(ResultCode.FAIL,e.getMessage(),null);
        }
    }

    /**
     * 用户注销
     * @param userId
     *
     */
    @IgnoreToken
//   @NeedAdmin
    @ApiOperation("用户注销")
    @ApiImplicitParam(name = "userId", value = "用户id", dataType = "String", paramType = "body", required = true)
    @PutMapping("/admins/manage/delUser")
    public  SysResult delUser(@RequestBody String userId){
        try{
            adminService.delUser(userId);
            return SysResult.build(ResultCode.CREATED,"用户注销成功",null);
        }catch (Exception e){
            return SysResult.build(ResultCode.FAIL,e.getMessage(),null);
        }
    }

    /**
     * 修改个人用户信息
     * @param personalUser
     *
     */
    @IgnoreToken
//   @NeedAdmin
    @ApiOperation("修改个人用户信息")
    @ApiImplicitParam(name = "userInfo", value = "个人用户信息", dataType ="PersonalUserVO", paramType = "body", required = true)
    @PutMapping("/admins/manage/modifyUserInfo")
    public SysResult modifyUserInfo(@RequestBody PersonalUserVO personalUser){
        try{
            adminService.modifyUserInfo(personalUser);
            return SysResult.build(ResultCode.CREATED,"修改个人用户信息成功",null);
        }catch (Exception e){
            return SysResult.build(ResultCode.FAIL,e.getMessage(),null);
        }
    }

    /**
     * 修改组织用户信息
     * @param orgUser
     *
     */
    @IgnoreToken
//   @NeedAdmin
    @ApiOperation("修改组织用户信息")
    @ApiImplicitParam(name = "orgInfo", value = "组织用户信息", dataType ="PersonalUserVO", paramType = "body", required = true)
    @PutMapping("/admins/manage/modifyOrgInfo")
    public SysResult modifyOrgInfo(@RequestBody OrganizerVO orgUser){
        try{
            adminService.modifyOrgInfo(orgUser);
            return SysResult.build(ResultCode.CREATED,"修改组织用户信息成功",null);
        }catch (Exception e){
            return SysResult.build(ResultCode.FAIL,e.getMessage(),null);
        }
    }

    /**
     * 修改赛事活动信息
     * @param match
     *
     */
    @IgnoreToken
//   @NeedAdmin
    @ApiOperation("修改赛事活动信息")
    @ApiImplicitParam(name = "orgInfo", value = "赛事活动信息", dataType ="MatchActivity", paramType = "body", required = true)
    @PutMapping("/admins/manage/modifyMatchInfo")
    public SysResult modifyMatchInfo(@RequestBody MatchActivity match){
        try{
            adminService.modifyMatchInfo(match);
            return SysResult.build(ResultCode.CREATED,"修改组织用户信息成功",null);
        }catch (Exception e){
            return SysResult.build(ResultCode.FAIL,e.getMessage(),null);
        }

    }
}